<?php 
namespace App\Services\User;

use App\Facades\Infra\User\WeChatManage;
use App\Facades\Services\User\WeChatService;
use App\Facades\Infra\User\UserManage;


class UserService{

	public function addUserInfo($userInfo){
		$openid = session('wechat.oauth_user')->getId();

		$conditions['openid'] = $openid;

		$wechatUser = WeChatManage::getUserInfo($conditions);

		if(count($wechatUser) == 0){
			WeChatService::updateUserInfo(session('wechat.oauth_user')->getOriginal());

			$wechatUser = WeChatManage::getUserInfo($conditions);
		}

		$userInfo['wechat_user_id'] = $wechatUser->id;

		UserManage::createUser($userInfo);
	}


	public function getUserInfo(){
		$openid = session('wechat.oauth_user')->getId();

		$conditions['openid'] = $openid;

		return UserManage::getUserInfo($conditions)->toArray()['wechat_user_info'];
	}

	public function getUser(){
		$openid = session('wechat.oauth_user')->getId();

		$conditions['openid'] = $openid;

		return UserManage::getUser($conditions);
	}

	public function updateUserInfo($data){
		$id = $data['id'];

		UserManage::updateUserInfo($id,$data);
	}
}